package org.xiaodongwei.jvm.op;

import org.xiaodongwei.jvm.exe.JVMStack;
import tech.medivh.classpy.classfile.bytecode.Branch;
import tech.medivh.classpy.classfile.bytecode.Instruction;
import tech.medivh.classpy.classfile.constant.ConstantPool;
import tech.medivh.classpy.classfile.jvm.Opcode;

public class IfIcmpleOpCodeExecute extends OpCodeExecute{

    public IfIcmpleOpCodeExecute(JVMStack jvmStack, ConstantPool constantPool) {
        this.opCode = Opcode.if_icmple;
        this.jvmStack = jvmStack;
        this.constantPool = constantPool;
    }

    @Override
    public void execute(Instruction instruction) {
        Object value2 = jvmStack.peek().getOperateStack().pop();
        Object value1 = jvmStack.peek().getOperateStack().pop();

        if (value1 instanceof Integer) {
            int v1 = (int) value1;
            int v2 = (int) value2;
            if (v1 <= v2) {
                Branch branch = (Branch) instruction;
                int jumpTo = branch.getJumpTo();
                jvmStack.peek().jumpTo(jumpTo);
            }
        } else if (value1 instanceof Float) {
            float v1 = (float) value1;
            float v2 = (float) value2;
            if (v1 <= v2) {
                Branch branch = (Branch) instruction;
                int jumpTo = branch.getJumpTo();
                jvmStack.peek().jumpTo(jumpTo);
            }
        }
    }
}
